﻿@using System.Globalization
@using System.Text
@using Eksponent.CropUp
@using Lu.Blog.Domain.Utils
@using Umbraco.Core
@using umbraco.MacroEngines
@using Umbraco.Web
@inherits Vega.USiteBuilder.UmbracoTemplatePageBase<Lu.Blog.Domain.Model.DocumentType.ContentPage>

@{
    ViewBag.BodyClass = "single";
    Layout = "_Master.cshtml";
}

<!-- MAIN -->
<div id="main">
    <div class="wrapper cf">
			
			
        <!-- masthead -->
        <div class="masthead cf">
            @(BlogContentHelper.GetRoot().ChildrenAsList.First(i => i.NodeTypeAlias == "HomePage").HasProperty("blogTitle") ? BlogContentHelper.GetRoot().ChildrenAsList.First(i => i.NodeTypeAlias == "HomePage").GetProperty("blogTitle").Value.ToUpper() : "{Blog Title}")
        </div>
        <!-- ENDS masthead -->
			
        <!-- posts list -->
        <div id="posts-list" class="cf">        	

            <!-- Standard -->
            <article class="format-standard">
                <div class="feature-image">
                    @{
                        string headerUrl = Model.Content.HasProperty("previewImage") ? XmlNodePickerHelper.GetImagesFromPicker(Model.Content.GetPropertyValue<string>("previewImage")).FirstOrDefault().NiceUrl : "";
                    }
                    <img src="@CropUp.GetUrl(headerUrl, new ImageSizeArguments {CropAlias = "postView"})" alt="Alt text" />
                </div>
                <div class="box cf">
                    @{
                        DateTimeFormatInfo frDtfi = new CultureInfo("fr-FR", false).DateTimeFormat;
                    }
                    <div class="entry-date"><div class="number">@Model.Content.CreateDate.Day</div><div class="month">@Model.Content.CreateDate.ToString("MMM", frDtfi).ToUpper()</div></div>
						
                    <div class="excerpt">
                        <div class="post-heading" >@(Model.Content.GetPropertyValue<string>("contentTitle").ToUpper())</div>
                        <div class="entry-content">
                            @Html.Raw(@Model.Content.GetPropertyValue<string>("contentBody"))
                        </div>
                    </div>
						
                    @{
                        var category = Model.Content.AncestorOrSelf("Category");
                        IEnumerable<DynamicNode> authors = XmlNodePickerHelper.GetContentNodesFromPicker(Model.Content.GetPropertyValue<string>("author"));
                    }

                    <div class="meta">
                        <span class="format">@(category.GetPropertyValue<string>("label"))</span>
                        <span class="user">By @Html.Raw(ShowAuthorWithLinksHtml(authors))</span>
                        @*<span class="comments">16 comments</span>*@
                        @*<span class="tags"><a href="#">red</a>, <a href="#">cyan</a>, <a href="#">white</a>, <a href="#">blue</a></span>*@
                    </div>
							
                </div>
            </article>
            <!-- ENDS  Standard -->
				
        </div>
        <!-- ENDS posts list -->
			
        <!-- sidebar -->
        <aside id="sidebar">
        	
            <ul>
                <li class="block">
                    <h4>@(category.GetPropertyValue<string>("label"))</h4>
                    @Html.Raw(category.GetPropertyValue<string>("description"))
                </li>
	        		
                <li class="block">
                    <h4>@Umbraco.GetDictionaryValue("Categories")</h4>
                    <ul>
                        @{
                            var categories = BlogContentHelper.GetAllNonEmptyCategories();
                            foreach (var cat in categories)
                            {
                                <li class="cat-item"><a href="@cat.NiceUrl" title="title">@cat.GetPropertyValue("label")<span class="post-counter"> (@cat.ChildrenAsList.Count())</span></a></li>
                            }
                        }
                    </ul>
                </li>
	        		
                <li class="block">
                    <h4>@Umbraco.GetDictionaryValue("Archives")</h4>
                    <ul>
                        @ShowArchive()
@*                      <li class="cat-item"><a href="#" title="title">January</a></li>
                        <li class="cat-item"><a href="#" title="title">February</a></li>
                        <li class="cat-item"><a href="#" title="title">March</a></li>*@
                    </ul>
                </li>
        		
            </ul>
        		
        </aside>
        <!-- ENDS sidebar -->
			
			
    </div><!-- ENDS WRAPPER -->
</div>
<!-- ENDS MAIN -->

@functions
{

    private string ShowAuthorWithLinksHtml(IEnumerable<DynamicNode> authors)
    {
        var ret = new StringBuilder();
        foreach (DynamicNode author in authors)
        {
            var strb = new StringBuilder();
            strb.Append("<a href=\"");
            strb.Append(author.NiceUrl);
            strb.Append("\">");
            strb.Append(author.GetPropertyValue("username"));
            strb.Append("</a>");
            ret.Append(strb);
            ret.Append(", ");
        }
        return ret.ToString().Substring(0, ret.ToString().Count()-2);
    }

    private string ShowArchive()
    {
        var posts = BlogContentHelper.GetAllPost();

        return "NotImplementedYet";
    }

}

